layui.use(['table', 'HttpRequest', 'func', 'form'], function () {

    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var form = layui.form;
    /**
     * 用户支付账单管理
     */
    var UserPayBills = {
        tableId: "userPayBillsTable"
    };

    /**
     * 初始化表格的列
     */
    UserPayBills.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'userId', sort: true, title: '用户ID'},
            {field: 'payBillsType', sort: true, title: '支付账单类型'},
            {field: 'tradeTotal', sort: true, title: '交易总数'},
            {field: 'incomeAmount', sort: true, title: '收入金额'},
            {field: 'incomeTradeCount', sort: true, title: '收入交易总数'},
            {field: 'expenditureAmount', sort: true, title: '支出金额'},
            {field: 'expenditureTradeCount', sort: true, title: '支出交易总数'},
            {field: 'payBillsStartTime', sort: true, title: '支付账单开始时间'},
            {field: 'payBillsEndTime', sort: true, title: '支付账单结束时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    UserPayBills.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(UserPayBills.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    UserPayBills.openAddDlg = function () {
        func.open({
            title: '添加用户支付账单',
            content: Feng.ctxPath + '/view/userPayBills/add',
            tableId: UserPayBills.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    UserPayBills.openEditDlg = function (data) {
        func.open({
            title: '修改用户支付账单',
            content: Feng.ctxPath + '/view/userPayBills/edit?id=' + data.id,
            tableId: UserPayBills.tableId
        });
    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    UserPayBills.onDeleteItem = function (data) {
        var operation = function () {
            var request = new HttpRequest(Feng.ctxPath + "/userPayBills/delete", 'post', function (data) {
                Feng.success("删除成功!");
                table.reload(UserPayBills.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            request.set("id", data.id);
            request.start(true);
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + UserPayBills.tableId,
        url: Feng.ctxPath + '/userPayBills/findPage',
        page: true,
        cellMinWidth: 100,
        cols: UserPayBills.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData,
        done: function(res, curr, count){
            $("[data-field = 'payBillsType']").children().each(function(){
                if($(this).text() == '1'){
                    $(this).text("支付宝支付");
                }else if($(this).text() == '0'){
                    $(this).text("微信支付");
                }
            })
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        UserPayBills.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        UserPayBills.openAddDlg();
    });

    // 添加按钮点击事件
    $('#btnUpload').click(function () {
        UserPayBills.btnUpload();
    });


    // 工具条点击事件
    table.on('tool(' + UserPayBills.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            UserPayBills.openEditDlg(data);
        } else if (layEvent === 'delete') {
            UserPayBills.onDeleteItem(data);
        }
    });
});
